<template>
	<view class="container">
		<view class="container-top">
			<view class="avatar" @click="showAvatars">
				<img :src="avatarImg">
			</view>
			<view class="name">{{name}}</view>
		</view>
		<tui-list-view title="菜单列表">
			<tui-list-cell @click="href('/pages/index/chatManage')" :arrow="true">
				<view class="tui-item-box">
					<tui-icon name="service-fill" :size="24" color="#5677fc"></tui-icon>
					<view class="tui-list-cell_name">管理聊天</view>
				</view>
			</tui-list-cell>
			<tui-list-cell @click="addChatVis=true;" :arrow="true">
				<view class="tui-item-box">
					<tui-icon name="explore-fill" :size="24" color="#19be6b"></tui-icon>
					<view class="tui-list-cell_name">添加聊天</view>
					<view class="tui-ml-auto">
						<tui-tag padding="10rpx 12rpx" margin="0 30rpx 0 0" size="24rpx" type="light-green" shape="circle">进行加密聊天</tui-tag>
					</view>
				</view>
			</tui-list-cell>
			<tui-list-cell @click="createChatVis=true;" :arrow="true">
				<view class="tui-item-box">
					<tui-icon name="explore-fill" :size="24" color="#19be6b"></tui-icon>
					<view class="tui-list-cell_name">创建聊天</view>
					<view class="tui-ml-auto">
						<tui-tag padding="10rpx 12rpx" margin="0 30rpx 0 0" size="24rpx" type="light-blue" shape="circle">自定义你的密钥</tui-tag>
					</view>
				</view>
			</tui-list-cell>
			<tui-list-cell @click="href('')" :arrow="true">
				<view class="tui-item-box">
					<tui-icon name="shop-fill" :size="23" color="#ed3f14"></tui-icon>
					<view class="tui-list-cell_name">功能介绍</view>
					<view class="tui-right">大脑升级</view>
				</view>
			</tui-list-cell>
			<tui-list-cell @click="toRegister()" :arrow="true" last="true">
				<view class="tui-item-box">
					<text class="tui-list-cell_name">注销</text>
					<view class="tui-right">更换账号</view>
				</view>
			</tui-list-cell>
		</tui-list-view>
		<tui-modal :zIndex="100" :maskZIndex="90" :show="addChatVis" :maskClosable="true" custom>
			<view class="tui-modal-custom">
				<view class="tui-modal-custom-text" style="text-align: center; font-size:18px;">加入聊天</view>
				<label>聊天室ID:</label>
				<input class="zy-input" placeholder="请输入ID" clearable v-model="addForm.chatId"></input>
				<label>密钥:</label>
				<input class="zy-input" label="密钥" placeholder="请输入密钥(口令)" clearable v-model="addForm.secondKey"></input>
				<tui-button height="72rpx"  class="zy-button" :size="28" type="danger" shape="circle" @click="joinChat()">确定</tui-button>
				<tui-button height="72rpx" class="zy-button" :size="28" shape="circle" style="margin-top:10px " @click="addChatVis=false">关闭</tui-button>
			</view>
		</tui-modal>
		<tui-modal :zIndex="100" :maskZIndex="90" :show="createChatVis" :maskClosable="true" custom>
			<view class="tui-modal-custom">
				<view class="tui-modal-custom-text" style="text-align: center; font-size:18px;">创建聊天</view>
				<label>聊天室名字:</label>
				<input class="zy-input" placeholder="请输入聊天名" clearable v-model="createForm.name"></input>
				<label>密钥:</label>
				<input class="zy-input" label="密钥" placeholder="请输入密钥(口令)" clearable v-model="createForm.secondKey"></input>
				<label>人数:</label>
				<input class="zy-input" label="密钥" placeholder="请输入人数" clearable v-model="createForm.amount"></input>
				<tui-button height="72rpx" class="zy-button" :size="28" type="danger" shape="circle" @click="createChat()">确定</tui-button>
				<tui-button height="72rpx" class="zy-button" :size="28"  shape="circle" style="margin-top:10px " @click="createChatVis=false">关闭</tui-button>
			</view>
		</tui-modal>
	</view>
</template>

<script>
	import md5 from 'md5';
	export default {
		data() {
			return {
				avatarImg:"https://pic1.zhimg.com/80/v2-3f4e944596b522ee31108d806e7884d2_720w.jpg?source=1940ef5c",
				addChatVis:false,
				createChatVis:false,
				name:"",
				addForm:{
					chatId:"",
					secondKey:""
				},
				createForm:{
					name:"",
					secondKey:"",
					amount:2
				}
			};
		},
		onShow: function() {},
		async created(){
			let res = await this.api.public.getUserInfo();
			uni.showToast({
				title:res.content,
				duration:500,
				icon:"none"
			});
			if(res.status){
				this.name = res.data.name;
				this.avatarImg = this.url+'/'+res.data.avatarImg;
			}
		},
		methods: {
			async joinChat(){
				let secondKey = md5(this.addForm.secondKey);
				let res = await this.api.public.joinChat({
					chatId:this.addForm.chatId,
					secondKey:secondKey
				});
				uni.showToast({
					title:res.content,
					duration:3000,
					icon:"none"
				});
				if(res.status){
					uni.setStorageSync(this.addForm.chatId,this.addForm.secondKey);
					this.addChatVis=false;
					this.addForm.chatId = "";
					this.addForm.secondKey = "";
				}
			},
			async createChat(){
				let secondKey = this.createForm.secondKey
				this.createForm.secondKey = md5(this.createForm.secondKey);
				let res = await this.api.public.createChat(this.createForm);
				
				uni.showToast({
					title:res.content,
					duration:3000,
					icon:"none"
				});
				if(res.status){
					uni.setStorageSync(res.data.id,secondKey);
					uni.showModal({
					    title: '提示',
					    content: `这是您创建聊天的频道ID'${res.data.id}',请收好!如果忘了可以去管理聊天界面再度查看`,
						confirmText:'我知道了!',
						cancelText:'别催了!',
					    success: (res) =>{
					       this.createChatVis=false;
						   this.createForm.name = "";
						   this.createForm.secondKey = "";
						   this.createForm.amount= 2;
					    }
					});
				}
			},
			href(url) {
				uni.navigateTo({
					url:url,
					animationType:'slide-in-right',
					animationDuration:2000
				})
			},
			toRegister(){
				uni.showModal({
					title:'是否确认注销?',
					content:'真的确定注销吗？这将会清除您的账号认证!',
					success:(res)=>{
						if(res.confirm){
							this.href('../login/index');
						}
					},
					fail:()=>{
						return;
					}
				})
			}
		},
	};
</script>

<style lang="scss" scoped>
	label{
		font-size:16px;
	}
	uni-toast {
		z-index: 10000;
	}
	.zy-button{
		text-align: center;
		margin-left:auto;
		margin-right:auto;
		width:200rpx;
	}
	.zy-input{
		margin:10px auto;
		border:1px solid #666;
		padding:5px;
		border-radius:10px;
		height:20px;
		font-size:16px;
		box-sizing: content-box;
	}
	.container{
		>.container-top{
			width:100%;
			padding-top:10px;
			padding-bottom:40px;
			background-color: #5c8dff;
			border-bottom-left-radius: 10%;
			border-bottom-right-radius: 10%;
			>.avatar{
				width:60px;
				height:60px;
				background-color: white;
				border-radius:60px;
				margin:auto;
				>img{
					width:60px;
					height:60px;
					border-radius: 60px;
				}
			}
			>.name{
				text-align: center;
				color:white;
				margin:10px auto;
			}
		}
	}
	.tui-item-box {
		width: 100%;
		display: flex;
		align-items: center;
	}
	
	.tui-list-cell_name {
		padding-left: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.tui-ml-auto {
		margin-left: auto;
	}
	
	.tui-right {
		margin-left: auto;
		margin-right: 34rpx;
		font-size: 26rpx;
		color: #999;
	}
	
	.tui-logo {
		height: 52rpx;
		width: 52rpx;
		flex-shrink: 0;
	}
	
	.tui-flex {
		display: flex;
		align-items: center;
	}
	
	.tui-msg-box {
		display: flex;
		align-items: center;
	}
	
	.tui-msg-pic {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		display: block;
		margin-right: 24rpx;
		flex-shrink: 0;
	}
	
	.tui-msg-item {
		max-width: 500rpx;
		min-height: 80rpx;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	
	.tui-msg-name {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		font-size: 34rpx;
		line-height: 1;
		color: #262b3a;
	}
	
	.tui-msg-content {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		font-size: 26rpx;
		line-height: 1;
		color: #9397a4;
	}
	
	.tui-msg-right {
		max-width: 120rpx;
		height: 88rpx;
		margin-left: auto;
		text-align: right;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
	}
	
	.tui-right-dot {
		height: 76rpx !important;
		padding-bottom: 10rpx !important;
	
	}
	
	.tui-msg-time {
		width: 100%;
		font-size: 24rpx;
		line-height: 24rpx;
		color: #9397a4;
	}
</style>
